package com.seg.web.controller.wms;

import com.seg.common.annotation.Log;
import com.seg.common.core.controller.BaseController;
import com.seg.common.core.domain.AjaxResult;
import com.seg.common.core.page.TableDataInfo;
import com.seg.common.enums.BusinessType;
import com.seg.common.utils.poi.ExcelUtil;
import com.seg.system.domain.TEquipment;
import com.seg.system.domain.TEquipmentV;
import com.seg.system.service.ITEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2023-06-26
 */
@RestController
@RequestMapping("/wms/equipment")
public class TEquipmentController extends BaseController {
    @Autowired
    private ITEquipmentService tEquipmentService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('wms:equipment:list')")
    @GetMapping("/list")
    public TableDataInfo list(TEquipment tEquipment) {
        startPage();
        List<TEquipment> list = tEquipmentService.selectTEquipmentList(tEquipment);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('wms:equipment:export')")
    @Log(title = "【导出设备列表】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TEquipment tEquipment) {
        List<TEquipment> list = tEquipmentService.selectTEquipmentList(tEquipment);
        List<TEquipmentV> vList = new ArrayList<>();
        if (list != null && !list.isEmpty()) {
            list.forEach(t -> {
                TEquipmentV v = new TEquipmentV();
                v.setSerialNo(t.getSerialNo());
                v.setEquipName(t.getEquipName());
                v.setLine(t.getLine());
                v.setStation(t.getStation());
                v.setQuantity(t.getQuantity());
                v.setUnit(t.getUnit());
                v.setStatus(String.valueOf(t.getStatus()));
                v.setSopTime(t.getSopTime());
                v.setRemark(t.getRemark());
                vList.add(v);
            });
        }
        ExcelUtil<TEquipmentV> util = new ExcelUtil<>(TEquipmentV.class);
        util.exportExcel(response, vList, "【导出设备】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('wms:equipment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tEquipmentService.selectTEquipmentById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('wms:equipment:add')")
    @Log(title = "【新增设备】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TEquipment tEquipment) {
        int i = tEquipmentService.insertTEquipment(tEquipment);
        if (i == -1) {
            return toAjax1(i, "设备编号已存在");
        }
        return toAjax(i);
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('wms:equipment:edit')")
    @Log(title = "【修改设备】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TEquipment tEquipment) {
        return toAjax(tEquipmentService.updateTEquipment(tEquipment));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('wms:equipment:remove')")
    @Log(title = "【删除设备】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tEquipmentService.deleteTEquipmentByIds(ids));
    }

    @PostMapping("/importTemplate")
    public void importSapTemplate(HttpServletResponse response) {
        ExcelUtil<TEquipmentV> util = new ExcelUtil<>(TEquipmentV.class);
        util.importTemplateExcel(response, "设备数据");
    }

    @Log(title = "设备管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('wms:equip:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<TEquipmentV> util = new ExcelUtil<>(TEquipmentV.class);
        List<TEquipmentV> itemList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = tEquipmentService.importData(itemList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/get_product_line")
    public TableDataInfo getProductLine() {
        return getDataTable(tEquipmentService.getProductLine());
    }

    @PostMapping("/get_station")
    public TableDataInfo getStation(@RequestParam(name = "line") String line) {
        return getDataTable(tEquipmentService.getStation(line));
    }

    @PostMapping("/get_status")
    public TableDataInfo getStatus() {
        return getDataTable(tEquipmentService.getStatus());
    }
    @PostMapping("/get_out_type")
    public TableDataInfo getOutType() {
        return getDataTable(tEquipmentService.getOutType());
    }
}
